﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DongDong.Common;
using System.Data;

public partial class GRZX_WSXY_PSDZ : Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string Check = Request["Check"];
            if (string.IsNullOrEmpty(Check))
            {
                SetMeta();
                PopulateDrp();
                PopulateList();
                Client objClient = Client.GetCurrent();
                if (objClient == null)
                    return;
                lblCount.Text = PSDZB.GetAllByAddUserID(objClient.ID).Count.ToString();

            }
            else
            {
                Response.Clear();
               
                if (Check == "GetCity")
                {
                    int ProID = Misc.GetQueryStringAsInt("ProID");
                    List<S_City> listCity = S_City.GetAllByCondition(ProID, "");
                    Response.Write(this.DataTableJson(listCity, null, 1));
                }
                else if (Check == "GetRegion")
                {
                    int CityID = Misc.GetQueryStringAsInt("CityID");
                    List<S_District> listDistrict = S_District.GetAllByCondition(CityID, "");
                    Response.Write(this.DataTableJson(null, listDistrict, 2));
                }
                else if (Check == "Save")
                {
                    btnSava();
                }
               
                Response.End();
            }
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        PopulateList();
    }
    public void PopulateList()
    {
        Client objClient = Client.GetCurrent();
        if (objClient == null)
            return;
        int CityID = Misc.GetFormValueAsInt("drpCity");
        int ReginID = Misc.GetFormValueAsInt("drpRegin");
        string Name = Misc.GetFormValue("txtKey").Trim();
        lstWDXX.DataSource = PSDZB.GetAllInUserIDByConditon(Misc.ConvertToInt(drpSheng.SelectedValue), CityID, ReginID, Name, (int)PSDZB.type.普通用户, "", "", "", objClient.ID);
        lstWDXX.DataBind();
    }

    public void PopulateDrp()
    {
        drpSheng.DataSource = S_Province.GetAll();
        drpSheng.DataTextField = "ProvinceName";
        drpSheng.DataValueField = "ProvinceID";
        drpSheng.DataBind();
        drpSheng.Items.Insert(0, new ListItem("请选择", "0"));

        drpAddProv.DataSource = S_Province.GetAll();
        drpAddProv.DataTextField = "ProvinceName";
        drpAddProv.DataValueField = "ProvinceID";
        drpAddProv.DataBind();

        drpAddCity.DataSource = S_City.GetAllByCondition(Misc.ConvertToInt(drpAddProv.SelectedValue), "");
        drpAddCity.DataTextField = "CityName";
        drpAddCity.DataValueField = "CityID";
        drpAddCity.DataBind();

        drpAddRegion.DataSource = S_District.GetAllByCondition(Misc.ConvertToInt(drpAddCity.SelectedValue), "");
        drpAddRegion.DataTextField = "DistrictName";
        drpAddRegion.DataValueField = "DistrictID";
        drpAddRegion.DataBind();
    }


    public void SetMeta()
    {
       
        this.SetMeta("配送地址管理-我是学员-求学网", "求学网_家教辅导_网上家教辅导_企业培训_个人培训_网上培训", "求学网是一个给您提供网上家教辅导的平台，方便您无论在哪里都能够学习到新的东西");

    }

    // 获得省市区名称
    public string GetShengShiQuName(object sheng, object shi, object qu)
    {
        string Return = "暂无资料";
        Return = S_District.GetPName_CName_RNameByPID_CID_RID(sheng, shi, qu);
        return Return;
    }


    protected void lstWDXX_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int ID = Misc.ConvertToInt(lstWDXX.DataKeys[e.ItemIndex].Value);
        PSDZB.Delete(ID);
        PopulateList();
    }
    protected void btnSava()
    {
        Client objClient = Client.GetCurrent();
        if (objClient == null)
            return;
        List<PSDZB> listPSDZB = PSDZB.GetAllByAddUserID(objClient.ID);
        if (listPSDZB.Count > 10)
        {
            Response.Write("false");
        }
        int ID = Misc.GetFormValueAsInt("OpeateID");
        string AddName = Server.UrlDecode(Misc.GetFormValue("UserName"));
        string AddPhone = Misc.GetFormValue("Phone");
        string AddTelphone = Misc.GetFormValue("Telphone");
        int AddProv = Misc.GetFormValueAsInt("ProvID");
        int AddCity = Misc.GetFormValueAsInt("CityID");
        int AddRegion = Misc.GetFormValueAsInt("RegionID");
        string AddAddress = Server.UrlDecode(Misc.GetFormValue("Address"));
        string AddCode = Misc.GetFormValue("Code");
        string IsRank = Misc.GetFormValue("IsRank");

        PSDZB objPSDZB = new PSDZB();
        if (ID != 0)
        {
            objPSDZB = PSDZB.GetByID(ID);
        }
        objPSDZB.ProvinceID = AddProv;
        objPSDZB.CityID = AddCity;
        objPSDZB.ReginID = AddRegion;
        objPSDZB.UserName = AddName;
        objPSDZB.Phone = AddPhone;
        objPSDZB.Telphone = AddTelphone;
        objPSDZB.Address = AddAddress;
        objPSDZB.Code = AddCode;
      
        if (IsRank == "checked")
        {
            objPSDZB.IsRank = true;

            PSDZB objPSDZBNow = PSDZB.GetIsRankByUserID(objClient.ID); ;
            objPSDZBNow.IsRank = false;
            objPSDZBNow.Update();

        }
        else
        {
            objPSDZB.IsRank = false;

        }
        if (ID != 0)
        {
            objPSDZB.Update();
        }
        else
        {
            objPSDZB.AddUserID = objClient.ID;
            objPSDZB.Type = (int)PSDZB.type.普通用户;
            objPSDZB.Enable_Flag = 1;

            objPSDZB.Add();
        }
        Response.Write("success");
    }
    protected void lstWDXX_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "SetRank")
        {
            Client objClient = Client.GetCurrent();
            if (objClient == null)
                return;

            PSDZB objPSDZB = PSDZB.GetIsRankByUserID(objClient.ID);
            objPSDZB.IsRank = false;
            objPSDZB.Update();
            int ID = Misc.ConvertToInt(e.CommandArgument);
            objPSDZB = PSDZB.GetByID(ID);
            objPSDZB.IsRank = true;
            objPSDZB.Update();

        }
    }
    protected void lstWDXX_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Label lblIsRank = (Label)e.Item.FindControl("lblIsRank");
            LinkButton lbtnSetRank = (LinkButton)e.Item.FindControl("lbtnSetRank");
            if (lblIsRank.Text != "True")
            {
                lblIsRank.Visible = false;

            }
            else
            {
                lbtnSetRank.Visible = false;
                lblIsRank.Text = "默认";
            }

        }
    }
}